﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITAP.ShprossesBuilder.Modelling.Components
{
    public static class ComponentListExtensionMethods
    {
        public static bool Contains(this List<IComponent> components, Type type)
        {
            foreach (IComponent component in components)
                if (component.GetType() == type)
                    return true;

            return false;
        }

        public static List<IComponent> GetComponentsByType(this List<IComponent> components, Type type)
        {
            List<IComponent> result = new List<IComponent>();
            foreach (IComponent component in components)
                if (component.GetType() == type)
                    result.Add(component);

            return result;
        }

        public static void MoveToStart(this List<IComponent> components, IComponent componentToMove)
        {
            int index = components.IndexOf(componentToMove);
            if (index <= 0)
                return;
            components.Remove(componentToMove);
            //components.Insert(index - 1, componentToMove);
            components.Insert(0, componentToMove);
        }

        public static void MoveToEnd(this List<IComponent> components, IComponent componentToMove)
        {
            int index = components.IndexOf(componentToMove);
            if (index == -1 || index == components.Count - 1)
                return;
            components.Remove(componentToMove);
            //components.Insert(index + 1, componentToMove);
            components.Insert(components.Count, componentToMove);

        }
    }
}
